package com.minarto.core {
	import scaleform.clik.core.CLIK;
	import flash.display.*;
	import com.minarto.data.*;
	
	/**
	 * @author KIMMINHWAN
	 */
	public class ClientBridge {
		static public const MODE_LOGIN:int = 0,
							MODE_SERVER_SELECT:int = 1,
							MODE_CHARACTER_CREATE:int = 10,
							MODE_CHARACTER_SELECT:int = 20, 
							MODE_GAME:int = 30;
		
		
		static private var _instance:ClientBridge = new ClientBridge;
		static public function getInstance():ClientBridge {
			return	_instance;
		}
		
		
		private var _mode:int = MODE_LOGIN;
		public function setMode($int:int = MODE_LOGIN):void {
			if ($int == _mode) {
				return;
			}
			
			_mode = $int;
		}
		
		
		public function ClientBridge() {
			if (_instance) {
				throw new Error("don't create instance");
			}
		}
		 
		private var _uiList:Vector.<UIData> = new <UIData>[];
		
		
		public function setUI(...$uis):void {
			delUI();
			_uiList.push($uis);
		}
		
		
		public function delUI():void {
			_uiList.length = 0;
		}
		
		
		public function getUI($uID:String):DisplayObject {
			var i:uint, m:DisplayObjectContainer, d:DisplayObject;
			
			
			m = CLIK.stage.getChildAt(0) as DisplayObjectContainer;
			i = m.numChildren;
			while (i -- ) {
				d = m.getChildAt(i);
				if ($uID == d.name) {
					return	d;
				}
			}
			
			return	null;
		}
		
		
		public function openUI($uiID:String):DisplayObject {
			var d:DisplayObject;
			
			CLIK.stage.getChildAt(0).visible = true;
			
			d = getUI($uiID);
			if(d){
				d.visible = true;
			}			
			
			return	d;
		}
		
		
		public function closeUI($uiID:String):DisplayObject {
			var d:DisplayObject = getUI($uiID);
			if(d){
				d.visible = false;
			}	
			
			return	d;
		}
		
		
		public function openUIAll():void {
			CLIK.stage.getChildAt(0).visible = true;
		}
		
		
		public function closeUIAll():void {
			CLIK.stage.getChildAt(0).visible = false;
		}
		
		
		private static var _datas:Vector.<GData> = new <GData>[];
		
		
		public function addData($data:GData):GData {
			_datas[_datas.length] = $data;
			
			return	$data;
		}
		
		public function addDatas(...$datas):void {
			_datas.push($datas);
		}
		
		
		public function update($data:GData, $property:String, $value:*):GData {
			$data[$property] = $value;
			
			return	$data;
		}
		
		
		public function updates($data:GData, ...$args):GData {
			var i:uint, c:uint;
			
			c = $args.length;
			while (i < c) {
				$data[$args[i]] = $args[i + 1];
				
				i += 2;
			}			
			
			return	$data;
		}
	}
}
